/***********************************************************************************/
/**///ҪעAHT20,AHT21ͺţACв졣AHT200x33,AHT210x00,SendAC(void)
/***********************************************************************************/
#include "stm32f10x.h" 


void Delay_N10us(uint32_t t)//ʱ
{
  uint32_t k;

   while(t--)
  {
    for (k = 0; k < 2; k++);//110
  }
}



void SensorDelay_us(uint32_t t)//ʱ
{
		
	for(t = t-2; t>0; t--)
	{
		Delay_N10us(1);
	}
}



void Delay_4us(void)		//ʱ
{	
	Delay_N10us(1);
	Delay_N10us(1);
	Delay_N10us(1);
	Delay_N10us(1);
}
void Delay_5us(void)		//ʱ
{	
	Delay_N10us(1);
	Delay_N10us(1);
	Delay_N10us(1);
	Delay_N10us(1);
	Delay_N10us(1);

}

void Delay_1ms(uint32_t t)		//ʱ
{
   while(t--)
  {
    SensorDelay_us(1000);//////ʱ1ms
  }
}


void AHT20_Clock_Init(void)		//ʱ
{
	RCC_APB2PeriphClockCmd(CC_APB2Periph_GPIOB,ENABLE);
}

void SDA_Pin_Output_High(void)   //PB15Ϊ  Ϊߵƽ PB15ΪI2CSDA
{
	GPIO_InitTypeDef  GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,& GPIO_InitStruct);
	GPIO_SetBits(GPIOB,GPIO_Pin_15);
}

void SDA_Pin_Output_Low(void)  //P15Ϊ  Ϊ͵ƽ
{

	GPIO_InitTypeDef  GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,& GPIO_InitStruct);
	GPIO_ResetBits(GPIOB,GPIO_Pin_15);
}

void SDA_Pin_IN_FLOATING(void)  //SDAΪ
{

	GPIO_InitTypeDef  GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;//
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init( GPIOB,&GPIO_InitStruct);
}

void SCL_Pin_Output_High(void) //SCLߵƽP14ΪI2CSCL
{
	GPIO_SetBits(GPIOB,GPIO_Pin_14);
}

void SCL_Pin_Output_Low(void) //SCL͵ƽ
{
	GPIO_ResetBits(GPIOB,GPIO_Pin_14);
}

void Init_I2C_Sensor_Port(void) //ʼI2Cӿ,Ϊߵƽ
{	
	GPIO_InitTypeDef  GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,& GPIO_InitStruct);
	GPIO_SetBits(GPIOB,GPIO_Pin_15);//ߵƽ
	
	GPIO_InitTypeDef  GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_14;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,& GPIO_InitStruct);
	GPIO_SetBits(GPIOB,GPIO_Pin_15);//ߵƽ
	
}




























void I2C_Start(void)		 //I2CSTARTź
{
	SDA_Pin_Output_High();
	SensorDelay_us(8);
	SCL_Pin_Output_High();
	SensorDelay_us(8);
	SDA_Pin_Output_Low();
	SensorDelay_us(8);
	SCL_Pin_Output_Low();
	SensorDelay_us(8);   
}


void AHT20_WR_Byte(uint8_t Byte) //AHT20дһֽ
{
	uint8_t Data,N,i;	
	Data=Byte;
	i = 0x80;
	for(N=0;N<8;N++)
	{
		SCL_Pin_Output_Low(); 
		Delay_4us();	
		if(i&Data)
		{
			SDA_Pin_Output_High();
		}
		else
		{
			SDA_Pin_Output_Low();
		}	
			
    SCL_Pin_Output_High();
		Delay_4us();
		Data <<= 1;
		 
	}
	SCL_Pin_Output_Low();
	SensorDelay_us(8);   
	SDA_Pin_IN_FLOATING();
	SensorDelay_us(8);	
}	


uint8_t AHT20_RD_Byte(void)//AHT20ȡһֽ
{
	uint8_t Byte,i,a;
	Byte = 0;
	SCL_Pin_Output_Low();
	SDA_Pin_IN_FLOATING();
	SensorDelay_us(8);	
	for(i=0;i<8;i++)
	{
    SCL_Pin_Output_High();		
		Delay_5us();
		a=0;
		if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)) a=1;
		Byte = (Byte<<1)|a;
		SCL_Pin_Output_Low();
		Delay_5us();
	}
  SDA_Pin_IN_FLOATING();
	SensorDelay_us(8);	
	return Byte;
}


uint8_t Receive_ACK(void)   //AHT20ǷлظACK
{
	uint16_t CNT;
	CNT = 0;
	SCL_Pin_Output_Low();	
	SDA_Pin_IN_FLOATING();
	SensorDelay_us(8);	
	SCL_Pin_Output_High();	
	SensorDelay_us(8);	
	while((GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15))  && CNT < 100) 
	CNT++;
	if(CNT == 100)
	{
		return 0;
	}
 	SCL_Pin_Output_Low();	
	SensorDelay_us(8);	
	return 1;
}

void Send_ACK(void)		  //ظACKź
{
	SCL_Pin_Output_Low();	
	SensorDelay_us(8);	
	SDA_Pin_Output_Low();
	SensorDelay_us(8);	
	SCL_Pin_Output_High();	
	SensorDelay_us(8);
	SCL_Pin_Output_Low();	
	SensorDelay_us(8);
	SDA_Pin_IN_FLOATING();
	SensorDelay_us(8);
}

void Send_NOT_ACK(void)	//ظACK
{
	SCL_Pin_Output_Low();	
	SensorDelay_us(8);
	SDA_Pin_Output_High();
	SensorDelay_us(8);
	SCL_Pin_Output_High();	
	SensorDelay_us(8);		
	SCL_Pin_Output_Low();	
	SensorDelay_us(8);
    SDA_Pin_Output_Low();
	SensorDelay_us(8);
}

void Stop_I2C(void)	  //һЭ
{
	SDA_Pin_Output_Low();
	SensorDelay_us(8);
	SCL_Pin_Output_High();	
	SensorDelay_us(8);
	SDA_Pin_Output_High();
	SensorDelay_us(8);
}

uint8_t AHT20_Read_Status(void)//ȡAHT20״̬Ĵ
{

	uint8_t Byte_first;	
	I2C_Start();
	AHT20_WR_Byte(0x71);
	Receive_ACK();
	Byte_first = AHT20_RD_Byte();
	Send_NOT_ACK();
	Stop_I2C();
	return Byte_first;
}

uint8_t AHT20_Read_Cal_Enable(void)  //ѯcal enableλûʹ
{
	uint8_t val = 0;//ret = 0,
  val = AHT20_Read_Status();
	 if((val & 0x68)==0x08)
		 return 1;
   else  return 0;
 }

void AHT20_SendAC(void) //AHT20AC
{

	I2C_Start();
	AHT20_WR_Byte(0x70);
	Receive_ACK();
	AHT20_WR_Byte(0xac);//0xACɼ
	Receive_ACK();
	AHT20_WR_Byte(0x33);	//AHT20ͺŴ˴33
//	AHT20_WR_Byte(0x00);	//AHT21ͺŴ˴00
	Receive_ACK();
	AHT20_WR_Byte(0x00);
	Receive_ACK();
	Stop_I2C();

}

//CRCУͣCRC8/MAXIM
//ʽX8+X5+X4+1
//Poly0011 0001  0x31
//λŵͱ 1000 1100 0x8c
//Cʵ룺
uint8_t Calc_CRC8(uint8_t *message,uint8_t Num)
{
        uint8_t i;
        uint8_t byte;
        uint8_t crc=0xFF;
  for(byte=0; byte<Num; byte++)
  {
    crc^=(message[byte]);
    for(i=8;i>0;--i)
    {
      if(crc&0x80) crc=(crc<<1)^0x31;
      else crc=(crc<<1);
    }
  }
        return crc;
}

void AHT20_Read_CTdata(uint32_t *ct) //ûCRCУ飬ֱӶȡAHT20¶Ⱥʪ
{
	uint8_t  Byte_1th=0;
	uint8_t  Byte_2th=0;
	uint8_t  Byte_3th=0;
	uint8_t  Byte_4th=0;
	uint8_t  Byte_5th=0;
	uint8_t  Byte_6th=0;
	uint32_t RetuData = 0;
	uint16_t cnt = 0;
	AHT20_SendAC();
	Delay_1ms(80);//ʱ80ms	
    cnt = 0;
	while(((AHT20_Read_Status()&0x80)==0x80))//ֱ״̬bit[7]Ϊ0ʾΪ״̬Ϊ1ʾæ״̬
	{
		SensorDelay_us(1508);
		if(cnt++>=100)
		{
		 break;
		 }
	}
	I2C_Start();
	AHT20_WR_Byte(0x71);
	Receive_ACK();
	Byte_1th = AHT20_RD_Byte();//״̬֣ѯ״̬Ϊ0x98,ʾΪæ״̬bit[7]Ϊ1״̬Ϊ0x1C0x0C0x08ʾΪ״̬bit[7]Ϊ0
	Send_ACK();
	Byte_2th = AHT20_RD_Byte();//ʪ
	Send_ACK();
	Byte_3th = AHT20_RD_Byte();//ʪ
	Send_ACK();
	Byte_4th = AHT20_RD_Byte();//ʪ/¶
	Send_ACK();
	Byte_5th = AHT20_RD_Byte();//¶
	Send_ACK();
	Byte_6th = AHT20_RD_Byte();//¶
	Send_NOT_ACK();
	Stop_I2C();

	RetuData = (RetuData|Byte_2th)<<8;
	RetuData = (RetuData|Byte_3th)<<8;
	RetuData = (RetuData|Byte_4th);
	RetuData =RetuData >>4;
	ct[0] = RetuData;//ʪ
	RetuData = 0;
	RetuData = (RetuData|Byte_4th)<<8;
	RetuData = (RetuData|Byte_5th)<<8;
	RetuData = (RetuData|Byte_6th);
	RetuData = RetuData&0xfffff;
	ct[1] =RetuData; //¶

}


void AHT20_Read_CTdata_crc(uint32_t *ct) //CRCУ󣬶ȡAHT20¶Ⱥʪ
{
	uint8_t  Byte_1th=0;
	uint8_t  Byte_2th=0;
	uint8_t  Byte_3th=0;
	uint8_t  Byte_4th=0;
	uint8_t  Byte_5th=0;
	uint8_t  Byte_6th=0;
	uint8_t  Byte_7th=0;
	uint32_t RetuData = 0;
	uint16_t cnt = 0;
	uint8_t  CTDATA[6]={0};//CRC
	
	AHT20_SendAC();//AHT20AC
	Delay_1ms(500);//ʱ500ms	
    cnt = 0;
	while(((AHT20_Read_Status()&0x80)==0x80))//ֱ״̬bit[7]Ϊ0ʾΪ״̬Ϊ1ʾæ״̬
	{
		SensorDelay_us(1508);
		if(cnt++>=100)
		{
		 break;
		}
	}
	
	I2C_Start();

	AHT20_WR_Byte(0x71);
	Receive_ACK();
	CTDATA[0]=Byte_1th = AHT20_RD_Byte();//״̬
	Send_ACK();
	CTDATA[1]=Byte_2th = AHT20_RD_Byte();//ʪ
	Send_ACK();
	CTDATA[2]=Byte_3th = AHT20_RD_Byte();//ʪ
	Send_ACK();
	CTDATA[3]=Byte_4th = AHT20_RD_Byte();//ʪ/¶
	Send_ACK();
	CTDATA[4]=Byte_5th = AHT20_RD_Byte();//¶
	Send_ACK();
	CTDATA[5]=Byte_6th = AHT20_RD_Byte();//¶
	Send_ACK();
	Byte_7th = AHT20_RD_Byte();//CRC
	Send_NOT_ACK();            //ע: ǷNAK
	Stop_I2C();
	
	if(Calc_CRC8(CTDATA,6)==Byte_7th)
	{
	RetuData = (RetuData|Byte_2th)<<8;
	RetuData = (RetuData|Byte_3th)<<8;
	RetuData = (RetuData|Byte_4th);
	RetuData =RetuData >>4;
	ct[0] = RetuData;//ʪ
	RetuData = 0;
	RetuData = (RetuData|Byte_4th)<<8;
	RetuData = (RetuData|Byte_5th)<<8;
	RetuData = (RetuData|Byte_6th);
	RetuData = RetuData&0xfffff;
	ct[1] =RetuData; //¶
		
	}
	else
	{
		ct[0]=0x00;
		ct[1]=0x00;//У󷵻ֵͻԸԼҪ
	}//CRC
}


void AHT20_Init(void)   //ʼAHT20
{	
	I2C_Start();
	AHT20_WR_Byte(0x70);
	Receive_ACK();
	AHT20_WR_Byte(0xbe);//0xBEʼAHT20ĳʼ0xBE
	Receive_ACK();
	AHT20_WR_Byte(0x08);//ؼĴbit[3]1ΪУ׼
	Receive_ACK();
	AHT20_WR_Byte(0x00);
	Receive_ACK();
	Stop_I2C();
	Delay_1ms(10);//ʱ10ms
}


int32_t main(void)
{
    uint32_t CT_data[2];
	volatile int  c1,t1;
	/***********************************************************************************/
	/**///ٸϵ磬ƷоƬڲҪʱ䣬ʱ100~500ms,500ms
	/***********************************************************************************/
	Delay_1ms(500);
	/***********************************************************************************/
	/**///ϵһη0x71ȡ״̬֣ж״̬ǷΪ0x18,0x18,мĴʼ
	/***********************************************************************************/
	if((AHT20_Read_Status()&0x18)!=0x18)
	{
	AHT20_Init(); //³ʼĴ
	Delay_1ms(10);
	}
	
	/***********************************************************************************/
	/**///۸ݿͻԼ󷢲ȡʪݣǰwhile1ѭȡʪݣο
	/***********************************************************************************/
	while(1)
	{
	 AHT20_Read_CTdata(CT_data);       //CRCУ飬ֱӶȡAHT20¶Ⱥʪ    Ƽÿ1Sһ
    //AHT20_Read_CTdata_crc(CT_data);  //crcУ󣬶ȡAHT20¶Ⱥʪ 
	

	 c1 = CT_data[0]*100*10/1024/1024;  //õʪֵc1Ŵ10
	 t1 = CT_data[1]*200*10/1024/1024-500;//õ¶ֵt1Ŵ10
	////һͻʾݣ
	 }

 }	
